package org.gap.jseed.apf;

import java.lang.annotation.Annotation;
import java.util.Set;

import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.PrimitiveType;

import org.gap.jseed.contract.annotation.NotNull;

public class NotNullProcessor implements AnnotatedMethodProcessor {

	@Override
	public Class<? extends Annotation> getSupportedAnnotation() {
		return NotNull.class;
	}

	@Override
	public void process(Set<? extends Element> elements, MessageWriter writer) {
		for (Element each : elements) {
			if (each instanceof ExecutableElement) {
				ExecutableElement exec = (ExecutableElement)each;
				if (exec.getParameters().isEmpty()) {
					writer.warningMessage(Messages.NotNull_NoParameters, exec);
					continue;
				}
				boolean hasNonPrimitiveType = false;
				for (Element element : exec.getParameters()) {
					if (!(element.asType() instanceof PrimitiveType)) {
						hasNonPrimitiveType = true;
					}
				}
				if (!hasNonPrimitiveType) {
					writer.warningMessage(Messages.NotNull_OnlyPrimitiveParameters, exec);
				}
			}
		}
	}
}
